"""
This module implements some useful function on pybookshelf related pathnames.
"""

import os, sys
import shutil
import random

home = os.path.expanduser('~')
config_dir = os.path.join(home, ".pybookshelf")
cache_dir = os.path.join(config_dir, "cache")
reports_dir = os.path.join(config_dir, "reports")

lib_dir = None
data_dir = None
locale_dir = None
#
firstrun = False

javascript_path = None
auto_css_path = None

def initialize(installed):
    """
    Initialize module: detect and remember some important paths.
    """
    global firstrun, data_dir, locale_dir, lib_dir
    
    if installed:
        # the variable below will be changed to correct prefix value on install
        prefix = "##PREFIX##"
        data_dir = os.path.join(prefix, "share", "pybookshelf")
        lib_dir = os.path.join(prefix, "lib", "pybookshelf")
        locale_dir = os.path.join(prefix, "share", "locale")
    else:
        # we are in the source tree so 
        base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
        lib_dir = base_dir 
        data_dir = base_dir
        locale_dir = os.path.join(base_dir, "po")

    firstrun = os.path.isdir(config_dir)
    if not firstrun:
        try:
            os.mkdir(config_dir)
        except:
            print >> sys.stderr, "Unable to create configuration directory"

def initialize_service_dir():
    """
    Initialize application service directory (usually "~/.pybookshelf"): create/update required files,
    clean cache etc.
    """
    # clean cache
    # FIXME: add error handler
    if os.path.exists(cache_dir):
        shutil.rmtree(cache_dir)
        
    os.mkdir(cache_dir)
    if not os.path.exists(reports_dir):
        os.mkdir(reports_dir)
        
    # re-generate css file
    from . import settings
    from . import html
    from gtk.gdk import color_parse
    
    normal_font = settings.get_str("style/normal_font")
    headers_font = settings.get_str("style/headers_font")
    background_color = settings.get_gdk_color("style/background_color")
    hyperlinks_color = settings.get_gdk_color("style/hyperlinks_color")
    hover_links_color = settings.get_gdk_color("style/hover_links_color")
    normal_text_color = settings.get_gdk_color("style/normal_text_color")
    html.update_auto_css_file(normal_font, headers_font, background_color, hover_links_color)

def get_config_path(*path_elements):
    return os.path.join(config_dir, *path_elements)

def get_data_path(*path_elements):
    """
        Returns filename from shared data directory, usually from `/usr/share/pybookshelf'
    """
    return os.path.join(data_dir, *path_elements)

def get_cache_path(*path_elements):
    """
        Returns full filename from cache
    """
    return os.path.join(cache_dir, *path_elements)

def make_unique_dir(root_path):
    """
        Create directory with unique name in the directory root_path, i.e. always
        create some new directory in root_path.
    """
    
    while True:
        rand_name_list = [random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for x in range(8)]
        rand_name = os.path.join(root_path, "".join(rand_name_list))
        
        if os.path.exists(rand_name):
            continue
        
        # pass possible IOError out of the function
        os.mkdir(rand_name)
        return rand_name
    
def javascript_file():
    global javascript_path
    
    if javascript_path is None:
        javascript_path = get_cache_path("javascript.js")
    
    return javascript_path

def auto_css_file():
    global auto_css_path
    
    if auto_css_path is None:
        auto_css_path = get_cache_path("auto.css")
        
    return auto_css_path











